package com.lechuang.cunpinke.reveiver;

import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.lechuang.cunpinke.manager.MyDownLoadManager;
import com.lechuang.cunpinke.manager.ToastManager;

/**
 * 下载状态监听
 *
 * 作者：尹松涛
 * 邮箱：yinsongtaoshmily@outlook.com
 * 日期：2017/9/26
 * 时间：11:33
 */

public class DownLoadReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
            long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1L);
            if (id == -1) {
                return;
            }
            int status = MyDownLoadManager.getInstance(context).queryDownLoadStatus(id);
            if (status == -1) {
                return;
            }
            if (status == DownloadManager.STATUS_FAILED) {
                ToastManager.getInstance().showLongToast("文件下载失败,请重新下载");
            } else if (status == DownloadManager.STATUS_SUCCESSFUL) {
                ToastManager.getInstance().showLongToast("文件下载成功");
            }
        }
    }
}
